package com.ffcs.crmd.tsp.server.log;

import java.lang.reflect.Method;
import java.net.URL;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.core.util.MixAll;
import com.ffcs.crmd.tsp.server.config.SchedulerConfig;

/**
 * 
 * 功能说明:客户端日志
 *
 * @author zhongfuhua
 * 
 * @Date 2016年2月3日 上午1:20:19
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
public class ServerLogger {
    
    protected ServerLogger() {
        
    }
    
    public static void init(SchedulerConfig schedulerConfig) throws Exception{
        createLogger(schedulerConfig);
    }
    
    private static void createLogger(SchedulerConfig schedulerConfig) throws Exception {
        try {
            ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
            Class<? extends ILoggerFactory> classType = iLoggerFactory.getClass();
            if (classType.getName().equals("org.slf4j.impl.Log4jLoggerFactory")) {
                
                // 反射获取初始化日志的对象
                Class<?> domConfigurator = Class.forName("org.apache.log4j.xml.DOMConfigurator");
                Object domConfiguratorObj = domConfigurator.newInstance();
                
                
                if(StringUtils.isBlank(schedulerConfig.getTspHome())){
                    Method configure = domConfiguratorObj.getClass().getMethod("configure", URL.class);
                    URL log4j_server = ServerLogger.class.getClassLoader().getResource(MixAll.TSP_SERVER_LOG4J_PAHT);
                    URL log4j_remoting= ServerLogger.class.getClassLoader().getResource(MixAll.TSP_SERVER_REMOTING_LOG4J_PAHT);
                    URL log4j_default = ServerLogger.class.getClassLoader().getResource(MixAll.TSP_SERVER_DEFAULT_LOG4J_PAHT);
                    configure.invoke(domConfiguratorObj, log4j_server);
                    configure.invoke(domConfiguratorObj, log4j_remoting);
                    if(Boolean.valueOf(System.getProperty(MixAll.TSP_SERVER_OPEN_DEFAULT_LOG, "false"))){
                        configure.invoke(domConfiguratorObj, log4j_default);
                    }
                    System.out.println("tsp logs loading default log4j config.....");
                }else{
                    Method configure = domConfiguratorObj.getClass().getMethod("configure", String.class);
                    configure.invoke(domConfiguratorObj, schedulerConfig.getTspHome() + "/conf/log4j_server.xml");
                    configure.invoke(domConfiguratorObj, schedulerConfig.getTspHome() + "/conf/log4j_remoting.xml");
                    if(Boolean.valueOf(System.getProperty(MixAll.TSP_SERVER_OPEN_DEFAULT_LOG, "false"))){
                        configure.invoke(domConfiguratorObj, schedulerConfig.getTspHome() + "/conf/log4j_default.xml");
                    }
                    System.out.println("tsp logs loading define log4j config.....");
                }
                
            } else if (classType.getName().equals("ch.qos.logback.classic.LoggerContext")) {
                
                // 反射获取初始化日志的对象
                Class<?> joranConfigurator = Class.forName("ch.qos.logback.classic.joran.JoranConfigurator");
                Class<?> context = Class.forName("ch.qos.logback.core.Context");
                Object joranConfiguratorObj = joranConfigurator.newInstance();
                
                // 添加日志上下文
                Method setContext = joranConfiguratorObj.getClass().getMethod("setContext",context);
                setContext.invoke(joranConfiguratorObj, iLoggerFactory);
                
                // 添加日志配置
                if(StringUtils.isBlank(schedulerConfig.getTspHome())){
                    Method doConfigure = joranConfiguratorObj.getClass().getMethod("doConfigure", URL.class);
                    URL logback_server = ServerLogger.class.getClassLoader().getResource(MixAll.TSP_SERVER_LOGBACK_PAHT);
                    URL logback_remoting= ServerLogger.class.getClassLoader().getResource(MixAll.TSP_SERVER_REMOTING_LOGBACK_PAHT);
                    URL logback_default= ServerLogger.class.getClassLoader().getResource(MixAll.TSP_SERVER_DEFAULT_LOGBACK_PAHT);
                    doConfigure.invoke(joranConfiguratorObj, logback_server);
                    doConfigure.invoke(joranConfiguratorObj, logback_remoting);
                    if(Boolean.valueOf(System.getProperty(MixAll.TSP_SERVER_OPEN_DEFAULT_LOG, "false"))){
                        doConfigure.invoke(joranConfiguratorObj, logback_default);
                    }
                    System.out.println("tsp logs loading default logback config.....");
                }else{
                    Method doConfigure = joranConfiguratorObj.getClass().getMethod("doConfigure", String.class);
                    doConfigure.invoke(joranConfiguratorObj, schedulerConfig.getTspHome() + "/conf/logback_server.xml");
                    doConfigure.invoke(joranConfiguratorObj, schedulerConfig.getTspHome() + "/conf/logback_remoting.xml");
                    if(Boolean.valueOf(System.getProperty(MixAll.TSP_SERVER_OPEN_DEFAULT_LOG, "false"))){
                        doConfigure.invoke(joranConfiguratorObj, schedulerConfig.getTspHome() + "/conf/logback_default.xml");
                    }
                    System.out.println("tsp logs loading define logback config.....");
                }
                
            }
        } catch (Exception e) {
            throw e;
        }
    }
    
}
